package com.example.app2

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.setupActionBarWithNavController

@SuppressLint("RestrictedApi")
class MainActivity : AppCompatActivity() {
    var navController: NavController? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //actionbarbn 也通过navctroller管理
        navController = findNavController(R.id.fragmentContainerView)
        setupActionBarWithNavController(navController!!)
        navController?.addOnDestinationChangedListener { controller, destination, arguments ->
//            Log.e("onBackPressed~~~~~~","${controller?.currentBackStackEntry?.destination?.getLabel()}")
        }

    }

    //使左上角的返回键起作用
    override fun onSupportNavigateUp(): Boolean {
        return navController?.navigateUp()!!
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {

        if(event.action == MotionEvent.ACTION_DOWN) {
            navController?.backStack?.forEach {
                Log.e("~~~~~~","${it.destination.label}")
            }
            return true
        }
       return false
    }
    @SuppressLint("RestrictedApi")
    override fun onBackPressed() {
        super.onBackPressed()
//        Log.e("onBackPressed~~~~~~","${navController?.currentBackStackEntry?.destination?.navigatorName}")
    }
}